#!/bin/bash
#install prometheus
wget -P /usr/local https://documentation-samples.obs.cn-north-4.myhuaweicloud.com/solution-as-code-publicbucket/solution-as-code-moudle/database-solution-for-om-platform/open-source-software/prometheus-2.25.0.linux-amd64.tar.gz
tar xf /usr/local/prometheus-2.25.0.linux-amd64.tar.gz -C /usr/local
mv /usr/local/prometheus-2.25.0.linux-amd64/ /usr/local/prometheus
{
  echo "[Unit]"
  echo "Description=https://prometheus.io"
  echo "[Service]"
  echo "ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090"
  echo "[Install]"
  echo "WantedBy=multi-user.target"
} >>/usr/lib/systemd/system/prometheus.service
systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service
#install node_exporter
wget -P /usr/local https://documentation-samples.obs.cn-north-4.myhuaweicloud.com/solution-as-code-publicbucket/solution-as-code-moudle/database-solution-for-om-platform/open-source-software/node_exporter-1.1.2.linux-amd64.tar.gz
tar xf /usr/local/node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-1.1.2.linux-amd64/ /usr/local/node_exporter
{
  echo "[Unit]"
  echo "Description=node_exporter"
  echo "After=network.target"
  echo "[Service]"
  echo "ExecStart=/usr/local/node_exporter/node_exporter"
  echo "Restart=on-failure"
  echo "[Install]"
  echo "WantedBy=multi-user.target"
} >>/usr/lib/systemd/system/node_exporter.service
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
{
  echo "  - job_name: 'linux'"
  echo "    static_configs:"
  echo "    - targets: ['localhost:9100']"
} >>/usr/local/prometheus/prometheus.yml
systemctl restart prometheus.service
#install grafana
cd /usr/local/ || exit
wget https://dl.grafana.com/oss/release/grafana-7.4.3-1.x86_64.rpm
yum -y install expect
/usr/bin/expect <<EOF
spawn yum -y install initscripts fontconfig
expect Is this ok [y/d/N]:
send "y\r"
expect eof
exit
EOF
yum install -y grafana-7.4.3-1.x86_64.rpm
systemctl enable grafana-server.service
systemctl start grafana-server.service
systemctl status grafana-server.service
#install influx
wget -P /usr/local/ https://dl.influxdata.com/influxdb/releases/influxdb-1.7.9-static_linux_amd64.tar.gz
tar -xzf /usr/local/influxdb-1.7.9-static_linux_amd64.tar.gz -C /usr/local
cd  /usr/local/influxdb-1.7.9-1/
/usr/bin/expect <<EOF
spawn ./influx -username 'rwuser' -password '$1' -host $2 -port 8635
expect ">"
send "auth rwuser $1\r"
expect ">"
send "CREATE DATABASE prometheus\r"
expect ">"
send "SHOW DATABASES\r"
expect eof
exit
EOF
{
  echo "remote_write:"
  echo " - url: \"http://$2:8635/api/v1/prom/write?db=prometheus&u=rwuser&p=$1\""
  echo "remote_read:"
  echo " - url: \"http://$2:8635/api/v1/prom/read?db=prometheus&u=rwuser&p=$1\""
} >>/usr/local/prometheus/prometheus.yml
systemctl restart prometheus.service